package com.sky.com.sky.web.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    SetmealService setmealService;


//    @PostMapping
//    public Result add(@RequestBody SetmealDTO setmealDTO) {
//        setmealService.add(setmealDTO);
//        return Result.success();
//    }


    /**
     * 根据套餐ID查询套餐+套餐菜品
     *
     * @param id
     * @return
     **/
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id){
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }


    /**
     * 套餐起售停售
     *
     * @param id
     * @param status
     * @return
     **/
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result startOrStop(@PathVariable("status") Integer status,@RequestParam Long id){
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealService.startOrStop(setmeal);
        return Result.success();
    }





    /**
     * 套餐修改
     *
     * @param setmealDTO
     * @return
     **/
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {
       setmealService.updateById(setmealDTO);
       return Result.success();
    }


    /**
     * 套餐批量删除
     *
     * @param ids
     * @return
     **/
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result deleteBatch(@RequestParam List<Long> ids) {
        setmealService.deleteBatch(ids);
        return Result.success();
    }


    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     **/
    @PostMapping
    @CacheEvict(value = "setmealCache",key = "#setmealDTO.category") //新增一个套餐原先的套餐分类就会改变
    public Result save(@RequestBody SetmealDTO setmealDTO){
        setmealService.save(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageDTO
     * @return
     **/
   @GetMapping("/page")
    public Result getPage(SetmealPageDTO setmealPageDTO){
       PageResult pageResult = setmealService.getPage(setmealPageDTO);
       return Result.success(pageResult);
   }

}